package Function;

import FCRendering.FCRBattleOrderConstant;
import Frame.battleanimation.ABattleAni;
import Frame.battleanimation.NpcJoinBattleAni;
import VWorldGameStudio.*;
import VWorldGameStudio.events.BattleEvent;
import VWorldGameStudio.events.SayEvent;
import utils.FCMixUtil;

import java.util.ArrayList;
import java.util.concurrent.CopyOnWriteArrayList;

import static FCRendering.FCRMaster.*;

public class FCBattleEventOrderHelper {

    /**
     * @param event
     * @param round 第几回合的开始
     */
    public void dealBattleEvent(BattleEvent event, int round) {
        if(event == null) return;
        ArrayList<FCBattleEventOrder> gets = event.getEventOrderByRound(round);
        fcv.getBattleSayText().clear();
        for (FCBattleEventOrder eventOrder : gets) {
            if (eventOrder.eventOrderType == FCBattleEventOrder.EventOrderTypeSay) {
                //TODO 将当前回合要讲的话全讲了
                FCEvent sayEevent = eventLib.getEventOfName(eventOrder.battleSayTextId);
                for (String str : fcv.getSaytext((SayEvent)sayEevent)) {
                    fcv.getBattleSayText().add(langLib.getNameOfId(str));
                }
            } else if (eventOrder.eventOrderType == FCBattleEventOrder.EventOrderTypeEndPlot) {
                //TODO 剧情结束了
                if(eventOrder.jumpPlotId != null && !eventOrder.jumpPlotId.equals("null")) {
                    fcv.setPlotiding1(eventOrder.jumpPlotId);
                }
            } else if (eventOrder.eventOrderType == FCBattleEventOrder.EventOrderTypeCtrl) {
                switch (eventOrder.eventOrderDetail) {
                    case FCBattleEventOrder.EventOrderTypeCtrl_Battle_NPC_In:
                        //红狼剧情战斗
                        String npcName = eventOrder.contrlNpc;
                        FCPlayerInfo npc = new FCPlayerInfo();
                        npc.setAliasUID("npc");
                        npc.setName(npcName);
                        npc.setPlayertank1("honglangcar");
                        npc.setBlood(9999);
                        npc.setBloodMax(999);
                        npc.setSpeed1(60);
                        FCMutilPlayCache.joinGame(npc);
                        //TODO 坦克也要备份进来,不然坦克画不出来
                        updateATank(npc);

                        FCPlayerBattleWrap wrap = new FCPlayerBattleWrap(npc.getAliasUID(), npc.getName(), fcv.getProportion(),
                                (int) (1920 * fcv.getProportion()), (int) (700 * fcv.getProportion()), (int) (96 * fcv.getProportion()), (int) (102 * fcv.getProportion()));
                        fcv.getBattleWraps().add(wrap);
                        wrap.setBattleAttackOrder(FCRBattleOrderConstant.Order_JOIN_IN);
                        wrap.setBattleDriverTank(true);//开坦克
                        FCBattleOrder order = new FCBattleOrder();
                        order.playerUid = npc.getAliasUID();
                        order.playerName = npc.getName();
                        order.battleType = 1;
                        fcv.getNpcOrders().add(order);

                        break;
                    case FCBattleEventOrder.EventOrderTypeCtrl_Battle_NPC_Out:

                        break;
                    case FCBattleEventOrder.EventOrderTypeCtrl_Battle_M_In:

                        break;
                    case FCBattleEventOrder.EventOrderTypeCtrl_Battle_M_Out:

                        break;
                }
            }
        }
    }



    public void dealNpcMonsterBattleEvent(FCBattleOrder order, int frameTimes) {
        FCPlayerBattleWrap wrap = FCPlayerBattleWrap.findBattleWrapByName(order.playerUid, order.playerName);
        if (order.battleAnis == null) {
            order.battleAnis = new CopyOnWriteArrayList<>();
            if (order.battleType == 1) {
                //操作人物
                if (wrap.getBattleAttackOrder() == FCRBattleOrderConstant.Order_JOIN_IN) {
                    NpcJoinBattleAni npcJoinBattleAni = new NpcJoinBattleAni(order.playerName);
                    order.battleAnis.add(npcJoinBattleAni);
                    order.frameTimes = npcJoinBattleAni.getAllFrames();
                }
            }
        }
        if (order.battleAnis != null) {
            for (ABattleAni battleAni : order.battleAnis) {
                battleAni.nextFrame(frameTimes);
            }
        }
    }

    public void dealNpcMonsterBattleEventEnd(FCBattleOrder order, int frameTimes) {
            //nothing
    }

    public void findAllNpcAndReadyThem() {
        ArrayList<FCPlayerBattleWrap> wrapArrayList = FCPlayerBattleWrap.findAllWrapByHostId("npc");
        for (FCPlayerBattleWrap w : wrapArrayList) {
            if (w.isBattleReady()) {
                return;
            }
            FCPlayerInfo pl = FCMutilPlayCache.findPlayerByName(w.getHostUid(), w.getName());
            if (pl != null && !pl.isDeath()) {
                //如果坐坦克就用主炮，否则就用手武器
                if (w.isBattleDriverTank()) {
                    w.setBattleAttackOrder(FCRBattleOrderConstant.Order_Attack);
                    w.setBattlePlayerSelectMenu(FCRBattleOrderConstant.BattleSelectMenuTankMainGun);
                    w.setBattlePlayerSelectMonster(findTargetMonster());
                    w.setBattleReady(true);
                } else {
                    w.setBattleAttackOrder(FCRBattleOrderConstant.Order_Attack);
                    if (pl.getArm() != null && !"null".equals(pl.getArm())) {
                        w.setUseWeaponType(itemLib.getItemOfID(pl.getArm()).getType2());
                        w.setBattlePlayerSelectMenu(FCRBattleOrderConstant.BattleSelectMenuArmGun);
                    } else {
                        w.setBattlePlayerSelectMenu(FCRBattleOrderConstant.BattleSelectMenuTushou);
                    }
                    w.setBattlePlayerSelectMonster(findTargetMonster());
                    w.setBattleReady(true);
                }
            }
        }
    }

    //找目标打
    private int findTargetMonster() {
        //直接找第一个未死的怪了
        for (int i = 0; i < fcv.getBattleMonster().size(); i++) {
            FCMonster f = fcv.getBattleMonster().get(i);
            if (!f.getIsDeath()) {
                return i;
            }
        }
        return 0;
    }

    private void updateATank(FCPlayerInfo playerInfo) {
        FCTank npctank = tankLib.getTankOfID(playerInfo.getPlayertank1(), playerInfo.getPlayertankHostId());
        if (npctank == null) {
            FCTank tank = tankLib.getTankOfID(playerInfo.getPlayertank1(), gameSave.HOST_UID);
            tank.setHostId(playerInfo.getAliasUID());
            tank.setPdir(playerInfo.getActionDir());
            tank.setPPianYiX(playerInfo.getPianYiX());
            tank.setPPianYiY(playerInfo.getPianYiY());
            tank.setPcoorX(playerInfo.getPlayer1CoorX());
            tank.setPcoorY(playerInfo.getPlayer1CoorY());
            tank.setPmapname(playerInfo.getMapname());
            tank.setPactionNum(playerInfo.getActionNum());
            tank.setVisable(0);
            //要
            playerInfo.setPlayertankHostId(playerInfo.getAliasUID());
            FCMutilPlayTankCache.updateAndAddTank(tank);
        }
    }

}